package co.fitstart.coach.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.BusinessException;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dynamic;
import co.fitstart.coach.dao.CoachDao;
import co.fitstart.coach.dao.CourseDao;
import co.fitstart.coach.service.GradeService;
import co.fitstart.entity.course.schedule.CourseSchedule;
import co.fitstart.entity.course.schedule.ScheduleStatus;
import co.fitstart.entity.principal.Principal;

/**
 * 
 * @author haiyan_xiao
 *
 */
@Service("gradeService")
public class GradeServiceImpl implements GradeService {

    @Dynamic
    private CourseDao courseDao;

    @Autowired
    private GenericDaoSupport genericDaoSupport;

    @Dynamic
    private CoachDao coachDao;

    /*
     * (non-Javadoc)
     * @see co.fitstart.coach.service.GradeService#listUngradedSchedule(co.fitstart.entity.principal.Principal)
     */
    public List<CourseSchedule> listUngradedSchedule(Principal coach) {
        return courseDao.listUngradedSchedule(coach, ScheduleStatus.FINISH);
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.coach.service.GradeService#listGradedSchedule(co.fitstart.entity.principal.Principal)
     */
    public List<CourseSchedule> listGradedSchedule(Principal coach) {
        return courseDao.listGradedSchedule(coach, ScheduleStatus.FINISH);
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.coach.service.GradeService#grade(co.fitstart.entity.course.schedule.CourseSchedule, int)
     */
    public void grade(CourseSchedule courseSchedule, int grade) {
        if(courseSchedule.getGrade() != null) {
            throw new BusinessException("当前课时已评分");
        } else {
            genericDaoSupport.update(courseSchedule.grade(grade));
        }
    }

}
